package querysrc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.subway.babynote.syncdata.*;

import serverdata.ServerDataRecord;
import serverdata.cache_Manager;



public class query_RegUser extends query{
	
	public query_RegUser(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}
	//return user_id
	public int Register(ClientDataRecord.usersRecord ur)
	{
		Connection con = null;
		ResultSet rs = null;
		querydb qdb =null;
		int iret = -1;
		try
		{
			boolean bret = true;
			con = this.init();	
			if(bret)
			{
				qdb = new querydb(con, sqlscripts.sql_reg_user_select_uid);				
				bret = qdb.prePS();				
			}
			if(bret)
			{
				ServerDataRecord.userAuthenticateRecord uar = new ServerDataRecord.userAuthenticateRecord(ur.imsi,ur.imei);
	    		
				qdb.ps.setString(1, ur.email);				
				rs = qdb.execPSQuery();
				if (!rs.next()){
					boolean needdelvisitor=false;
					//new visitor
					iret = this.checkvisitor(con,ur);
					if(iret <0)
					{
						iret = IdManager.user_id.getnewid();
						ur.user_id = iret;
					}					
					else
						needdelvisitor=true;
					//insert								
					if(this.insertuserinfo(con,ur) >=0)
					{
						cache_Manager.get_userCache().putValue(ur.user_id, uar);
						if (needdelvisitor)
							this.deletevisitor(con,ur.user_id);
					}
					
			      }
			      else {    	 
			    	  int user_id = rs.getInt(sqlscripts.sql_reg_user_select_uid_c1_uid);
			    	  String imsi = rs.getString(sqlscripts.sql_reg_user_select_uid_c2_imsi);
			    	  String imei = rs.getString(sqlscripts.sql_reg_user_select_uid_c3_imei);
			    	  String passwd = rs.getString(sqlscripts.sql_reg_user_select_uid_c4_passwd);		    	  
			    	  
			    	  if(ur.passwd.equals(passwd))
			    	  {
			    		  cache_Manager.get_userCache().putValue(user_id, uar);
			    		  
			    		  iret = user_id;
			    		  if(ur.imsi.equals(imsi) && ur.imei.equals(imei))
		    			  {//nothing todo
			    			  ur.user_id = user_id;		    			  
		    			  }
			    		  else  //update imsi, imei ????
			    		  {
			    			  
			    		  }			    			  
			    	  }
			    	  else
			    		  iret = errMessage.err_mailexist.errcode; //mail existing, use another mail address
			        }
				
								
				
			}
			
			return iret;
			
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
			this.release_connection(con);
			this.finalize();
		}
	}
	
	private int checkvisitor(Connection con , ClientDataRecord.usersRecord ur)
	{
		querydb qdb = null;
		ResultSet rs = null;
		try
		{
			boolean bret = true;			
			qdb = new querydb(con,sqlscripts.sql_reg_user_visitor_check);
			bret = qdb.prePS();			
			if(bret)
			{
				qdb.ps.setInt(1, ur.user_id);
				rs = qdb.execPSQuery();
				
				if(!rs.next())
				{
					ur.user_id = -1;
				}
				else
				{
					String imsi = rs.getString(sqlscripts.sql_reg_user_visitor_check_c1_imsi);
					String imei = rs.getString(sqlscripts.sql_reg_user_visitor_check_c2_imei);
					java.sql.Date first_use_time = rs.getDate(sqlscripts.sql_reg_user_visitor_check_c3_first_use_time);
					if(ur.imsi.equals(imsi) && ur.imei.equals(imei))
					{
						//existing ??
						ur.first_use_time = first_use_time;
					}
					else
						ur.user_id = -1;
				}
				
								
			}					
			return ur.user_id;			
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
		}
	}
	private int deletevisitor(Connection con ,int visitor_id)
	{
		querydb qdb = null;		
		try
		{
			boolean bret = true;			
			qdb = new querydb(con,sqlscripts.sql_del_visitor);
			bret = qdb.prePS();
			int iret = 0;
			if(bret)
			{
				qdb.ps.setInt(1, visitor_id);
				iret = qdb.execPSInsert();
				// check return value
												
			}				
			return iret;			
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_querydb(qdb);
		}
	}
	
	private int insertuserinfo(Connection con ,ClientDataRecord.usersRecord ur)
	{
		//(user_id,imsi,imei,phone_number,first_use_time,birth,gender,email,passwd,first_reg_time)
		querydb qdb = null;
		try
		{
			boolean bret = true;
			int iret = -1;
			qdb = new querydb(con,sqlscripts.sql_reg_user_insert_uid);
			bret = qdb.prePS();
			if(bret)
			{
				qdb.ps.setInt(1, ur.user_id);
				qdb.ps.setString(2, ur.imsi);
				qdb.ps.setString(3,ur.imei);
				qdb.ps.setString(4, ur.phone_number);
				qdb.ps.setString(5, StringUtil.date2str(ur.first_use_time));
				qdb.ps.setDate(6, ur.birth);
				qdb.ps.setBoolean(7, ur.gender);
				qdb.ps.setString(8,ur.email);
				qdb.ps.setString(9, ur.passwd);
				qdb.ps.setString(10, StringUtil.date2str(ur.first_reg_time));
				qdb.ps.setInt(11, ur.ts);
				iret = qdb.execPSInsert();				
			}
				
			return iret;			
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_querydb(qdb);
		}
	}

}
